package com.project.controller;


import com.project.pojo.User;
import com.project.service.UserService;
import com.project.utils.JwtUtil;
import com.project.utils.ResponseData;
import com.project.utils.ResponseDataUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping("/login/loginByPwd")
    ResponseData<Map> loginByPwd(@RequestBody User user){
        System.out.println("前端发送请求");
        System.out.println(user.getPhone()+"assdsfd"+user.getPassword());
        User result=userService.loginByPassword(user);
        if (result!=null){
            Map<String,String> info =new HashMap<>();
            info.put("id",String.valueOf(result.getId()));
            String jwt= JwtUtil.createJWT(UUID.randomUUID().toString(),String.valueOf(result.getId()),JwtUtil.JWT_TTL);
            info.put("token",jwt);

            return ResponseDataUtil.buildOk("登录成功",info);
        }

//        return ResponseDataUtil.buildError("登录失败!");
        return new ResponseData<Map>(100,"登录失败!");


    }


    @PostMapping("/registerByPwd")
    ResponseData<User> registerByPwd(@RequestBody User user){
        if (userService.RegisterByPassword(user)){

            return ResponseDataUtil.buildOk(200,"注册成功!");
        }

        return ResponseDataUtil.buildError("注册失败!");
    }
}
